package com.brayan.webapp.controller;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import com.brayan.webapp.model.Pub;
import com.brayan.webapp.model.PubThread;
import com.brayan.webapp.repository.PubRepository;
import com.brayan.webapp.repository.PubThreadRepository;

@ManagedBean
@ViewScoped
public class PubThreadController implements Serializable {

	private static final long serialVersionUID = 1548789365619247682L;

	@Inject
	private PubThreadRepository pubThreadRepository;
	@Inject
	private PubRepository pubRepository;
	@Inject
	private PubThread pubThread;

	/**
	 * The PubController manages the entire conversation in a view scope. the
	 * passed param of the pubId is catched on creation.
	 */
	public PubThreadController() {
		String threadIdStr = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("tid");
		if (threadIdStr != null && !threadIdStr.isEmpty())
			id = Long.valueOf(threadIdStr);
	}

	@PostConstruct
	public void init() {
		if (getId() != null) {
			pubThread = pubThreadRepository.getPubThreadById(getId());
			if (pubThread == null) {
				// TODO Return "no pub found"
			}
		}
	}

	/**
	 * This initializing method will not reload an entity, once loaded. To
	 * reload use {@link #reInit(Long)}
	 */
	public void init(Long id) {
		setId(id);
		init();
	}

	private Long id;
	private String name;

	private List<Pub> relatedPubs;
	private Calendar lastChangeOfRecord;

	public Long getId() {
		return id;
	}

	private Long setId(Long id) {
		this.id = id;
		return this.id;
	}

	public String getName() {
		if (pubThread != null)
			this.name = pubThread.getName();
		return this.name;
	}

	public void setName(String name) {
		if (pubThread != null)
			pubThread.setName(name);
	}

	public List<Pub> getRelatedPubs() {
		this.relatedPubs = pubRepository.getPubsByThread(getId());
		return this.relatedPubs;
	}

	public Calendar getLastChangeOfRecord() {
		return lastChangeOfRecord;
	}

	public void setLastChangeOfRecord(Calendar lastChangeOfRecord) {
		this.lastChangeOfRecord = lastChangeOfRecord;
	}

	/**
	 * Returns threads, which are from the same category
	 * 
	 * @param You
	 *            must provide the thread id. See
	 *            {@link #getPubThreadsOfSameCategory()} if not required.
	 * @return
	 */
	public List<PubThread> getPubThreadsOfSameCategory(Long threadId) {
		return pubThreadRepository.getPubThreadsOfSameCategory(threadId);
	}

	/**
	 * Returns threads, which are from the same category
	 * 
	 * @return List of pub threads in the same category like the current thread.
	 */
	public List<PubThread> getPubThreadsOfSameCategory() {
		return pubThreadRepository.getPubThreadsOfSameCategory(getId());
	}

}
